<?php

namespace backend\modules\bigdata_fico_early_warning\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use backend\modules\bigdata_fico_early_warning\models\FicoEarlyWarning;

/**
 * FicoEarlyWarningSearch represents the model behind the search form about `backend\modules\bigdata_fico_early_warning\models\FicoEarlyWarning`.
 */
class FicoEarlyWarningSearch extends FicoEarlyWarning
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'import_type'], 'integer'],
            [['mobile', 'idcard', 'ret_code', 'score_id', 'err_msg', 'reason', 'risk_level', 'other_data', 'resp_data', 'created_at'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = FicoEarlyWarning::find();

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        $query->andFilterWhere([
            'id' => $this->id,
//            'created_at' => $this->created_at,
            'import_type' => $this->import_type,
        ]);

        $query->andFilterWhere(['like', 'mobile', $this->mobile])
            ->andFilterWhere(['like', 'idcard', $this->idcard])
            ->andFilterWhere(['like', 'ret_code', $this->ret_code])
            ->andFilterWhere(['like', 'score_id', $this->score_id])
            ->andFilterWhere(['like', 'err_msg', $this->err_msg])
            ->andFilterWhere(['like', 'reason', $this->reason])
            ->andFilterWhere(['like', 'risk_level', $this->risk_level])
            ->andFilterWhere(['like', 'other_data', $this->other_data])
            ->andFilterWhere(['like', 'resp_data', $this->resp_data]);

        if ( ! is_null($this->created_at) && strpos($this->created_at, ' - ') !== false ) {
            list($start_date, $end_date) = explode(' - ', $this->created_at);
            $query->andFilterWhere(['between', 'created_at', strtotime($start_date), strtotime($end_date.' 23:59:59')]);
        }

        return $dataProvider;
    }
}
